package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjCustomer;
import com.zb.entity.ZjCustomerInfo;
import com.zb.entity.vo.*;
import com.zb.mapper.ZjCustomerMapper;
import com.zb.service.SetService;
import com.zb.service.ZjCustomerInfoService;
import com.zb.service.ZjCustomerService;
import com.zb.service.ZjSystemServiceLogService;
import com.zb.utils.PageUtil;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 商家表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-07
 */
@Service
public class ZjCustomerServiceImpl extends ServiceImpl<ZjCustomerMapper, ZjCustomer> implements ZjCustomerService {
    @Autowired
    private ZjCustomerMapper customerMapper;
    @Autowired
    private SetService setService;
    @Autowired
    private ZjCustomerInfoService customerInfoService;
    @Autowired
    private ZjSystemServiceLogService systemServiceLogService;

    // 获取所有企业
    @Override
    public Map<String, Object> getListCustomer(CustomerSelectVo selectVo) {
        PageUtil.setPageLimit(selectVo);
        List<CustomerVo> voList = customerMapper.getListCustomer(selectVo);
        voList.forEach(t -> t.setCode(t.getCode().substring(0, 6) + "******" + t.getCode().substring(t.getCode().length() -6)));
        return PageUtil.pageList(selectVo.getPage(), selectVo.getLimit(), customerMapper.getListCustomerCount(selectVo), voList);
    }

    // 获取企业商家企业信息
    @Override
    public CustomerInfoVo getInfo(Long id) {
        ZjCustomer customer = this.getById(id);
        Assert.isTrue(null != customer, "参数[" + id + "]的数据不存在！", null);
        CustomerInfoVo vo = new CustomerInfoVo();
        BeanUtils.copyProperties(customer, vo);
        vo.setCode(vo.getCode().substring(0, 6) + "******" + vo.getCode().substring(vo.getCode().length() -6));
        vo.setCreateTime(customer.getGmtCreate());
        vo.setUpdateTime(customer.getGmtUpdate());
        return vo;
    }

    // 审核开通企业
    @Override
    @Transactional
    public void checkOpen(CustomerCheckOpenVo checkOpenVo) {
        Assert.isTrue(null != customerMapper.existNotCheckById(checkOpenVo.getId()), "该企业已审核！", null);
        ZjCustomer customer = new ZjCustomer();
        customer.setId(checkOpenVo.getId());
        customer.setStatus(2);
        customer.setOpenTime(checkOpenVo.getOpenTime());
        customer.setEndTime(checkOpenVo.getEndTime());
        this.updateById(customer);

        ZjCustomerInfo info = new ZjCustomerInfo();
        info.setId(checkOpenVo.getId());
        info.setUsers(checkOpenVo.getUsers());
        info.setVersionId(checkOpenVo.getVersionId());
        info.setVersion(checkOpenVo.getVersion());
        info.setSmsBalance(checkOpenVo.getSmsBalance());
        customerInfoService.updateById(info);
        // 初始化系统设置数据
        setService.initSystemSet(checkOpenVo.getId());
    }

    // 冻结企业
    @Override
    public void customerFreeze(int status, CustomerFreezeVo freezeVo) {
        List<ZjCustomer> customers = new ArrayList<>();
        ZjCustomer customer = null;
        for (Long id : freezeVo.getIdList()) {
            customer = new ZjCustomer();
            customer.setId(id);
            customer.setStatus(status);
            customers.add(customer);
        }
        this.updateBatchById(customers);
        systemServiceLogService.save(freezeVo.getIdList(), status == 1? "企业待审核" : status == 2? "企业使用中" : status == 3? "冻结企业" : "企业已过期", freezeVo.getRemark(), freezeVo.getAnnexs());
    }

    // 续期(续费、延长到期时间)
    @Override
    public void customerRenewal(CustomerRenewalVo renewalVo) {
        Assert.isTrue(null != renewalVo.getCustAccIdList() && !renewalVo.getCustAccIdList().isEmpty(), "至少要选择一个企业！", null);

        QueryWrapper<ZjCustomer> wrapper = new QueryWrapper<>();
        wrapper.in("id", renewalVo.getCustAccIdList());
        List<ZjCustomer> customerList = this.list(wrapper);

        List<ZjCustomer> customers = new ArrayList<>();
        ZjCustomer customer = null;

        for (ZjCustomer cu : customerList) {
            customer = new ZjCustomer();
            customer.setId(cu.getId());
            customer.setEndTime(renewalTime(cu.getEndTime(), renewalVo.getType(), renewalVo.getQty()));
            customers.add(customer);
        }
        this.updateBatchById(customers);
    }

    /**
     * 续期时间计算
     * @param endTime 到期时间
     * @param type 续期类型：1-天，2-季度，3-年度
     * @param qty 续期类型值
     * @return
     */
    private Date renewalTime(Date endTime, Integer type, Integer qty) {
        switch (type) {
            case 1 -> { // 天
                return DateUtils.addDays(endTime, qty);
            }
            case 2 -> { // 季度
                return DateUtils.addMonths(endTime, qty * 3);
            }
            case 3 -> { // 年度
                return DateUtils.addYears(endTime, qty);
            }
            default -> {
                return endTime;
            }
        }
    }


}
